package org.example.reactive.demo1.handler;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;


/**
 * @author Gong.Yang
 */
@Slf4j
@Configuration
@RequiredArgsConstructor
public class RouterConfig {
    private final EmployerHandler employerHandler;
    private final CreateErrorHandler errorHandler;
    private final ScopeTestHandler scopeTestHandler;


    @Bean
    public RouterFunction<ServerResponse> employerRouter() {
        return RouterFunctions.route(RequestPredicates.GET("/employerhandler/{id}"), employerHandler::findById);
    }

    @Bean
    public RouterFunction<ServerResponse> errorCreateRouter() {
        return RouterFunctions.route(RequestPredicates.GET("/baseError"), errorHandler::baseError)
                .andRoute(RequestPredicates.GET("/systemError"), errorHandler::systemError);
    }

    @Bean
    public RouterFunction<ServerResponse> scopeTestRouter() {
        return RouterFunctions.route(RequestPredicates.GET("/scope/path1"), x -> scopeTestHandler.print(x, "router1"))
                .filter(((request, next) -> scopeTestHandler.getFilterFunction(request, next, "拦截器1")))
                .andRoute(RequestPredicates.GET("/scope/path2"), x -> scopeTestHandler.print(x, "router2"))
                .filter(((request, next) -> scopeTestHandler.getFilterFunction(request, next, "拦截器2")))
                .andRoute(RequestPredicates.GET("/scope/path3"), x -> scopeTestHandler.print(x, "router3"))
                ;
    }
}
